import query from '@dfeidao/atom-nodejs/db/query';
// import format from '@dfeidao/atom-nodejs/dt/format';
// import query from '@dfeidao/atom-nodejs/db/query';
import { IncomingHttpHeaders } from 'http';
import { systemid } from '../atom/config';

interface Message {
	filter: {
		teacher_no: string;
		student_no: string;
		date: number;
		section: number;
	};
	page_size: number;
	page_no: number;
	productid: string;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string[];
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

export default async function atom(msg: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {

	const { prepare: p, exec: e } = await query(action_id, session_id, systemid);
	// 1.依据"学生编号" 查询<学生家长>表，获取对应的“家长编号parent_number
	p('parent_information', ['parent_number', 'parent_name', 'student_no', 'student_name', 'phone'], { student_no: msg.filter.student_no, is_message_receiver: '1', productid: msg.productid }, 10, 1, [], []);
	// 2.依据"信息模板类型" = "5","信息模板状态"="1" 查询<信息模板>表,得到"信息模板内容"
	p('information_template', ['information_template_content'], { information_template_type: '5', information_template_status: '1', productid: msg.productid }, 10, 1, [], []);

	const [data1, data2] = await e<{ phone: string }, { date: number, dateing: string }>();
	const usercode = data1[0].phone;
	// const usercode = '18738555317';

	// 3.得到的“手机 phone”查<pub_user_thirdpart_openid（12库）>表，获取对应的“openid”,
	const { prepare, exec } = await query(action_id, session_id, '12');

	prepare('pub_user_thirdpart_openid', ['openid'], { usercode, productid: msg.productid }, 10, 1, [], []);
	const [data3] = await exec();

	return { data: { data1: data1[0], data2: data2[0], data3: data3[0] } } as IWebResult;






	// // *****************************
	// // 2.依据"日期"、"学生编号"、"节次"查询<课后点评>表判断是否获取到数据
	// p('review_after_class', ['review_num', 'review_content', 'subject_name', 'student_name', 'grade', 'class_name', 'section', 'date'], { student_no: msg.filter.student_no, date: msg.filter.date, section: msg.filter.section }, 10, msg.page_no, [], []);

	// const [tear, stu] = await e<{ teacher_name: string }, { date: number, dateing: string }>();
	// console.log('123456', stu);
	// // tear[0].headimg = file_url + '/getfile?productid=' + spaceid + '&id=' + tear[0].headimg;
	// if (stu.length !== 0) {
	// 	stu[0].dateing = format(stu[0].date, 'YYYY/MM/DD');
	// 	return { data: { tea: tear[0], stu: stu[0] } } as IWebResult;
	// } else {
	// 	// 学生编号"查询<学生>表获取"学生姓名"
	// 	p('student', ['student_name'], { student_no: msg.filter.student_no }, 10, msg.page_no, [], []);
	// 	console.log('444', 11, msg.filter.section, msg.filter.teacher_no);

	// 	// 依据"日期''、"教师编号"、"节次"查询<上课记录>表
	// 	p('class_record', ['grade', 'class_name', 'subject_name', 'section', 'date'], { teacher_no: msg.filter.teacher_no, date: msg.filter.date, section: msg.filter.section }, 10, msg.page_no, [], []);
	// 	const [stu1, stu2] = await e<{ student_name: string }, { student_name: string }>();
	// 	stu2[0].student_name = stu1[0].student_name;
	// 	return { data: { tea: tear[0], stu: stu2[0] } } as IWebResult;

	// }




	// return { data: { tea: tear[0], stu: stu[0], stun: stun[0] } } as IWebResult;
}
